﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server.ActivityFeed;
using ESF.SharePoint.ActivityAggregators;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using ESF.SharePoint.Entities;
using ESF.SharePoint.ActivityAggregators.Providers;
using ESF.SharePoint.Configuration;
using ESF.SharePoint.Utility;

namespace ESF.SharePoint.Domain
{
    public class SocialFeeds
    {
        public ActivityEventsCollection GetActivitiesFromMyNetwork()
        {
            // Get ServiceLocator instance and obtain specified service instance
            IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
            IQueryActivity queryActivityService = serviceLocator.GetInstance<IQueryActivity>();
            
            // Get feeds
            ActivityEventsCollection activities = queryActivityService.GetActivitiesForMe();

            // Process feeds using provider engine
            return this.Process(activities);
        }

        internal ActivityEventsCollection Process(ActivityEventsCollection sourceActivities)
        {
            // Get providers to work on feed
            //ActivityEventsCollection activities = sourceActivities;
            //foreach (IActivityAggregatorProvider provider in ActivityAggregatorProviderManager.Providers)
            //{
            //    provider.CollectActivities(activities);
            //    provider.CompactActivites();
            //    activities = provider.GetNewActivities();
            //}

            return sourceActivities;
        }

        public List<ESF.SharePoint.Entities.CustomActivityEvent> GetActivitiesFromMyNetworkEx()
        {
            // Get ServiceLocator instance and obtain specified service instance
            IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
            IQueryActivity queryActivityService = serviceLocator.GetInstance<IQueryActivity>();

            // Get feeds
            ActivityEventsCollection activities = queryActivityService.GetActivitiesForMe();

            // Decouple SharePoint entity to custom entity
            List<CustomActivityEvent> list = ESF.SharePoint.Mappers.ActivityEventCollectionMapper.MapList(activities);

            // Process feeds using provider engine
            return this.ProcessEx(list);
        }

        internal List<ESF.SharePoint.Entities.CustomActivityEvent> ProcessEx(List<ESF.SharePoint.Entities.CustomActivityEvent> sourceActivities)
        {
            // Get providers to work on feed
            List<ESF.SharePoint.Entities.CustomActivityEvent> activities = sourceActivities;
            foreach (IActivityAggregatorProvider provider in ActivityAggregatorProviderManager.Providers)
            {
                activities = provider.ProcessActivities(activities);
            }

            return activities;
        }

        public string GetActivityFormat(ActivityType activityType)
        {
            try
            {
                //string templateId = Convert.ToString(activityType.ActivityTypeId);
                //ActivityTemplateElement activityTemplate = null;
                //foreach(ActivityTemplateElement template in ActivityAggregatorProviderManager.ActivityTemplates)
                //{
                //    if (template.TemplateId == templateId)
                //    {
                //        activityTemplate= template;
                //        break;
                //    }
                //}
                //var activityTemplate = from p in ActivityAggregatorProviderManager.ActivityTemplates[]
                //                        where template = templateId
                //                        select;
                //if (activityTemplate != null)
                return ESFUtility.GetLocalizedString("TemplateId_" + activityType.ActivityTypeId);
            }
            catch { return null; }
        }
    }
}
